﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using OpenComposite.EII;
using OpenComposite.EII.Designer.Workflow;
using OpenComposite.EII.Forms;
using OpenComposite.EII.Repository;

namespace OpenComposite.Repository.TFS
{
	public static class TeamFoundationWrapper
	{
		public static bool SelectSourceControl(
			ref string servername, ref string workspacename, ref string path)
		{
			OpenFileDialogEx dlg = new OpenFileDialogEx();
			dlg.AllowTfsSupport = true;
			dlg.DisableFileSystemSupport = true;
			dlg.OpenFolder = true;
			dlg.FileName = path;
			try {
				if ( !string.IsNullOrEmpty(servername) ) {
					TeamFoundationServer server = new TeamFoundationServer(servername);
					dlg.TfsUri = server.Uri.AbsolutePath;
					dlg.Workspace = workspacename;
				}
			} catch ( Exception ex ) {
				Debug.WriteLine(ex.ToString());
			}
			if ( dlg.ShowDialog() == DialogResult.OK ) {
				servername = dlg.TfsUri;
				workspacename = dlg.Workspace;
				path = dlg.FileName;
				return true; // EXIT
			}
			return false; // EXIT
		}
		public static void CheckForTfs()
		{
			try {
				TeamFoundationServer tfs = new TeamFoundationServer("OpenComposite");
			} catch ( Exception ex ) {
				Debug.WriteLine(ex.ToString());
			}
		}
		public static void ConnectAndCheckIntoTfs(WorkflowController wfcontroller,
			string serverPath, string localPath)
		{
			TeamFoundationServer tfServer = null;
			try {
				tfServer = new TeamFoundationServer(
					wfcontroller.Workflow.ExtendedProperties.SourceControlServer);
			} catch ( Exception ex ) {
				Debug.WriteLine(ex.ToString());
				return; // EXIT 
			}
			if ( tfServer == null ) return; // EXIT

			VersionControlServer versionControl = tfServer.GetVersionControl();
			if ( versionControl == null ) return; // EXIT

			Workspace workspace = versionControl.GetWorkspace(
				wfcontroller.Workflow.ExtendedProperties.SourceControlTfsWorkspace,
				tfServer.AuthenticatedUserName);
			if ( workspace == null ) return; // EXIT

			int addedFiles = workspace.PendAdd(localPath, true);
			PendingSet[] pendingSets = workspace.QueryPendingSets(
				null, RecursionType.Full, workspace.Name, tfServer.AuthenticatedUserName, true);

			workspace.Map(serverPath, localPath);

			PendingChange[] changes = workspace.GetPendingChanges(localPath, RecursionType.Full, true);
			if ( wfcontroller.Workflow.ExtendedProperties.CheckInOnDeploy ) {
				workspace.CheckIn(changes, "");
			}
		}

		public static TeamFoundationServer GetTeamFoundationServer(string uriTfs)
		{
			TeamFoundationServer tfs = null;
			if ( !string.IsNullOrEmpty(uriTfs) ) {
				try {
					tfs = new TeamFoundationServer(uriTfs);
				} catch ( Exception ex ) {
					Debug.WriteLine(ex.ToString());
					tfs = null;
				}
			}
			return tfs;
		}
		public static Workspace GetWorkspace(TeamFoundationServer tfs, string workspaceName)
		{
			if ( tfs == null ) throw new ArgumentNullException("tfs"); // ERROR

			VersionControlServer vcs = tfs.GetVersionControl();
			if ( vcs == null ) return null;	// EXIT

			Workspace ws = vcs.GetWorkspace(workspaceName, tfs.AuthenticatedUserName);
			return ws;
		}
		public static Workspace GetWorkspace(string uriTfs, string workspaceName)
		{
			TeamFoundationServer tfs = GetTeamFoundationServer(uriTfs);
			if ( tfs == null ) return null; // EXIT

			return GetWorkspace(tfs, workspaceName);
		}

		public static string GetLocalItemForServerItem(WebService webService, string serverItem)
		{
			return GetLocalItemForServerItem(
				webService.ExtendedProperties.TfsServerUri,
				webService.ExtendedProperties.TfsWorkspaceName,
				serverItem);
		}
		public static string GetLocalItemForServerItem(
			string uriTfs, string workspaceName, string serverItem)
		{
			Workspace ws = GetWorkspace(uriTfs, workspaceName);
			if ( ws == null ) return null; // EXIT

			return ws.GetLocalItemForServerItem(serverItem);
		}

		public static VersionControlServer GetVersionControl(this TeamFoundationServer tfServer)
		{
			return tfServer.GetService<VersionControlServer>();
		}
	}
}
